<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /* const obj = {
        b: 2,
      };

      Object.defineProperty(obj, "a", {
        get() {
          console.log("运行了属性a的get属性");
          return obj._a;
        },
        set(val) {
          console.log("运行了属性a的set属性", val);
          obj._a = val;
        },
      });

      obj.a = 20;
      console.log(obj.a); */

      const obj = {
        name: "zs",
      };

      Object.defineProperty(obj, "age", {
        get() {
          return this._age;
        },

        set(val) {
          if (val < 0) {
            val = 0;
          } else if (val > 200) {
            val = 200;
          }

          this._age = val;
        },
      });

      obj.age = 1000;
      console.log(obj.age);
    </script>
  </body>
</html>
